/**
 * @fileOverview link
 * @author huangtonger@aliyun.com
 */
const Mixin = {};

Mixin.CFG = {
  /**
   * 是否允许连接节点
   * @type {boolean}
   */
  linkNode: true,

  /**
   * 是否允许连接锚点
   * @type {boolean}
   */
  linkAnchor: true
};

Mixin.INIT = '_initLink';
Mixin.AUGMENT = {
  _initLink() {
    const graph = this.getGraph();
    const linkAnchor = this.get('linkAnchor');
    const linkNode = this.get('linkNode');
    const inputMode = graph.get('mode');

    if (linkAnchor) {
      this.on('beforeitemactived', ev => {
        const item = ev.item;
        if (item.isNode) {
          this.hoverShowAnchor(item);
        }
      });
      this.on('beforeitemunactived', ev => {
        const item = ev.item;
        if (item.isNode || item.isGroup) {
          this.clearAnchor(item);
        }
      });
      this.on('beforeitemselected', ev => {
        const item = ev.item;
        if (item.isNode) {
          this.hoverShowAnchor(item);
        } else if (item.isGroup) {
          this.hoverShowAnchor(item);
        }
      });
      this.on('beforeitemunselected', ev => {
        const item = ev.item;
        if (item.isNode || item.isGroup) {
          this.clearAnchor(item);
        }
      });
      graph.addBehaviour('dragAnchorAddEdge', 'add');
      graph.addBehaviour('hoverAnchorActived', 'default');
      graph.changeMode(inputMode);
    }
    if (linkNode) {
      graph.addBehaviour('hoverNodeAddOutter', 'add');
      graph.addBehaviour('hoverNodeAddOutter', 'default');
      graph.changeMode(inputMode);
    }
  }
};
module.exports = Mixin;
